package com.zxjbyte.yiyi.framework.jackson.module;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.zxjbyte.yiyi.framework.jackson.serializer.BigNumberSerializer;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @Author zhangxingjia
 * @Date 2023/3/1 17:54
 * @Version: 1.0
 */
public class YiyiNumberModule extends SimpleModule {

    public static final YiyiNumberModule INSTANCE = new YiyiNumberModule();

    public YiyiNumberModule() {
        super(YiyiNumberModule.class.getName());
        // Long 和 BigInteger 采用定制的逻辑序列化，避免超过js的精度
        this.addSerializer(Long.class, BigNumberSerializer.instance);
        this.addSerializer(Long.TYPE, BigNumberSerializer.instance);
        this.addSerializer(BigInteger.class, BigNumberSerializer.instance);
        // BigDecimal 采用 toString 避免精度丢失，前端采用 decimal.js 来计算。
        this.addSerializer(BigDecimal.class, ToStringSerializer.instance);
    }
}
